#include "keyboard/keyboard.h"
#include "appdata/app_data.h"
#include "motor/motor.h"
#include "Task_events.h"
BleKeyboard bleKeyboard("SmartKB32", "JJW", 100); // 其中“JJw键盘”为键盘名称；"JJW"为制造商
uint32_t Skyeisle_time = 0;
bool Skyeisle = 0;
uint8_t act_key = 0;
bool dial_or_lvgl = KNOB_LVGL; // lvgl:1,dial:0
uint8_t key_use_map = KEY_FAST_MAP;
extern lv_obj_t *SKY_PAGE;

#define QUEUE_SIZE 10                // 消息队列大小
static QueueHandle_t myQueue = NULL; // 声明一个队列句柄

void keyboard_init(void *pvParameters)
{
      espnowbegin();
      bleKeyboard.begin();
      bool begin_computer = 1;
      uint32_t delay_touch_id_time = millis();

      myQueue = xQueueCreate(QUEUE_SIZE, sizeof(uint32_t)); // 创建一个大小为 QUEUE_SIZE 的队列，用于存储 uint32_t 类型的数据
      if (myQueue == NULL)
      {
            // 错误处理：队列初始化失败
      }
      while (true)
      {
            uint8_t data_74hc165d = kb_data();
            if (data_74hc165d != 255)
            {
                  if (data_74hc165d != 0 and data_74hc165d <= KEY_DOWN_X6Y4)
                  {
                        if (bleKeyboard.isConnected())
                        {
                              switch (key_use_map)
                              {
                              case KEY_FAST_MAP:
                                    if (key_fast_map[data_74hc165d - 1][0])
                                          bleKeyboard.press(key_fast_map[data_74hc165d - 1][0]);
                                    if (key_fast_map[data_74hc165d - 1][1])
                                          bleKeyboard.press(key_fast_map[data_74hc165d - 1][1]);
                                    if (key_fast_map[data_74hc165d - 1][2])
                                          bleKeyboard.press(key_fast_map[data_74hc165d - 1][2]);
                                    break;
                              case KEY_NUM_MAP:
                                    if (key_num_map[data_74hc165d - 1][0])
                                          bleKeyboard.press(key_num_map[data_74hc165d - 1][0]);
                                    if (key_num_map[data_74hc165d - 1][1])
                                          bleKeyboard.press(key_num_map[data_74hc165d - 1][1]);
                                    if (key_num_map[data_74hc165d - 1][2])
                                          bleKeyboard.press(key_num_map[data_74hc165d - 1][2]);
                                    break;
                              }
                        }
                        else
                              Serial.println("未连接蓝牙");
                  }
                  else if (data_74hc165d >= KEY_UP_X2Y1 and data_74hc165d <= KEY_UP_X6Y4)
                        bleKeyboard.releaseAll();
                  else if (data_74hc165d <= KEY_UP_X7Y1)
                  {
                        switch (data_74hc165d)
                        {
                              /****************************************
                               *电脑APP打开控制
                               *****************************************/
                        case KEY_UP_X1Y2:
                              bleKeyboard.press(KEY_LEFT_CTRL); // 打开QQ对话框
                              bleKeyboard.press(KEY_LEFT_ALT);
                              bleKeyboard.press('z');
                              bleKeyboard.releaseAll();
                              break;
                        case KEY_UP_X1Y3:
                              bleKeyboard.press(KEY_LEFT_CTRL); // 打开微信对话框
                              bleKeyboard.press(KEY_LEFT_ALT);
                              bleKeyboard.press('x');
                              bleKeyboard.releaseAll();
                              break;
                        case KEY_UP_X1Y4:
                              bleKeyboard.press(KEY_MEDIA_LOCAL_MACHINE_BROWSER); // 打开“我的电脑”
                              bleKeyboard.releaseAll();
                              break;

                              /****************************************
                               *电脑媒体控制
                               *****************************************/
                        case KEY_DOWN_X1Y5:
                              bleKeyboard.press(KEY_MEDIA_VOLUME_DOWN); // 电脑音量减
                              break;
                        case KEY_UP_X1Y5:
                              bleKeyboard.release(KEY_MEDIA_VOLUME_DOWN);
                              break;
                        case KEY_DOWN_X2Y5:
                              bleKeyboard.press(KEY_MEDIA_VOLUME_UP); // 电脑音量加
                              break;
                        case KEY_UP_X2Y5:
                              bleKeyboard.release(KEY_MEDIA_VOLUME_UP);
                              break;
                        }
                        switch (data_74hc165d)
                        {
                              /****************************************
                               *灯光模式控制
                               *****************************************/
                        case KEY_DOWN_X3Y5:
                              key_board_change_lamp_state(lamp_brightness_lower, key_press);
                              break;
                        case KEY_UP_X3Y5:
                              key_board_change_lamp_state(lamp_brightness_lower, key_square);
                              break;
                        case KEY_DOWN_X4Y5:
                              key_board_change_lamp_state(lamp_brightness_add, key_press);
                              break;
                        case KEY_UP_X4Y5:
                              key_board_change_lamp_state(lamp_brightness_add, key_square);
                              break;
                        case KEY_DOWN_X5Y5:
                              key_board_change_lamp_state(lamp_colort_lower, key_press);
                              break;
                        case KEY_UP_X5Y5:
                              key_board_change_lamp_state(lamp_colort_lower, key_square);
                              break;
                        case KEY_DOWN_X6Y5:
                              key_board_change_lamp_state(lamp_colort_add, key_press);
                              break;
                        case KEY_UP_X6Y5:
                              key_board_change_lamp_state(lamp_colort_add, key_square);
                              break;
                              /****************************************
                               *桌面电源控制
                               *****************************************/
                        case KEY_DOWN_X7Y2:
                              USBdata_refresh.USB_OUT1 = !USBdata_refresh.USB_OUT1;
                              tx_onedata_to_USB();
                              break;
                        case KEY_DOWN_X7Y3:
                              USBdata_refresh.USB_OUT2 = !USBdata_refresh.USB_OUT2;
                              tx_onedata_to_USB();
                              break;
                        case KEY_DOWN_X7Y4:
                              USBdata_refresh.USB_OUT3 = !USBdata_refresh.USB_OUT3;
                              tx_onedata_to_USB();
                              break;
                        case KEY_DOWN_X7Y5:
                              USBdata_refresh.USB_OUT4 = !USBdata_refresh.USB_OUT4;
                              tx_onedata_to_USB();
                              break;

                              /****************************************
                               *其他控制
                               *****************************************/
                        case KEY_DOWN_X7Y1:
                              SKY_running = 0;
                              break;
                        case KEY_UP_X7Y1: // *dial/lvgl切换按钮
                              dial_or_lvgl = !dial_or_lvgl;
                              if (dial_or_lvgl)
                                    update_motor_mode(motor_mode[1], motor_mode[2]);
                              else
                              {
                                    update_motor_mode(MOTOR_SUPER_DIAL, 0);
                                    xTaskCreatePinnedToCore(Dynamic_Island_DL,  // 函数指针，任务函数
                                                            "Dynamic IslandDL", // 灵动岛  ，任务名字
                                                            1024 * 8,           // 栈大小
                                                            NULL,               // 获取地址传递参数
                                                            11,                 // 任务优先级, with 3 (configMAX_PRIORITIES - 1) 是最高的，0是最低的.
                                                            NULL,               // 任务句柄
                                                            0);                 // 内核
                              }
                              break;
                        case KEY_UP_X1Y1:
                              key_use_map == KEY_FAST_MAP ? key_use_map = KEY_NUM_MAP : key_use_map = KEY_FAST_MAP;
                              // SKY_running = 0;
                              // delay(10);

                              key_use_map == KEY_FAST_MAP
                                  ? xTaskCreatePinnedToCore(Dynamic_Island, "Dynamic Island", 1024 * 6, (void *)"key num map", 12, &handleTasksky, 0)
                                  : xTaskCreatePinnedToCore(Dynamic_Island, "Dynamic Island", 1024 * 6, (void *)"key def map", 12, &handleTasksky, 0);
                              break;
                        }
                  }
                  ////////////////双按键   仅用于屏幕弹窗/////////////////
                  ////和后面的   获取时间  调试用   冲突

                  else if (data_74hc165d >= DOUB_KEY_DOWN_X2Y1 and data_74hc165d <= DOUB_KEY_DOWN_X6Y4)
                  {
                        SKY_running = 0;
                        if (key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][0])
                              bleKeyboard.press(key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][0]);
                        if (key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][1])
                              bleKeyboard.press(key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][1]);
                        if (key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][2])
                              bleKeyboard.press(key_doub_map[data_74hc165d - DOUB_KEY_DOWN_X2Y1][2]);
                        bleKeyboard.releaseAll();
                  }
                  else if (data_74hc165d >= DOUB_KEY_UP_X2Y1 and data_74hc165d <= DOUB_KEY_UP_X6Y4)
                  {
                        switch (data_74hc165d)
                        {
                        case DOUB_KEY_UP_X5Y2:
                              xTaskCreatePinnedToCore(Dynamic_Island, "Dynamic Island", 1024 * 6, (void *)"Next piece of music", 12, &handleTasksky, 0);
                              break;
                        case DOUB_KEY_UP_X3Y2:
                              xTaskCreatePinnedToCore(Dynamic_Island, "Dynamic Island", 1024 * 6, (void *)"Previous music", 12, &handleTasksky, 0);
                              break;
                        }
                  }
                  else
                  {
                        switch (data_74hc165d)
                        {
                        case DOUB_KEY_UP_X1Y2: // 切换至默认映射表
                              key_use_map == KEY_FAST_MAP;

                              break;
                        case DOUB_KEY_UP_X1Y3: // 切换至数字映射表
                              key_use_map == KEY_NUM_MAP;
                              break;
                        case DOUB_KEY_UP_X1Y4: // 切换至APP映射表
                              key_use_map == KEY_APP1_MAP;
                              break;
                        case DOUB_KEY_UP_X1Y5:                 // 一键电脑休眠
                              bleKeyboard.press(KEY_LEFT_GUI); // 休眠
                              bleKeyboard.press('l');
                              bleKeyboard.releaseAll();

                              writeSD_off_state(); // 保存物联网设备状态信息
                              USBdata_refresh.USB_OUT1 = 0;
                              USBdata_refresh.USB_OUT2 = 0;
                              USBdata_refresh.USB_OUT3 = 0;
                              USBdata_refresh.USB_OUT4 = 0;
                              USBdata_refresh.oled = 0;
                              tx_onedata_to_USB(); // 关闭桌面电源
                              delayMicroseconds(10);
                              lampdata_refresh.brightness = 0;
                              lampdata_refresh.colorT = 127;
                              tx_lampdata_to_SmartLamp("KB");               // 关闭台灯
                              esp_sleep_enable_ext0_wakeup(GPIO_NUM_18, 0); // 主控进入深度睡眠
                              esp_deep_sleep_start();
                              break;
                        case DOUB_KEY_UP_X2Y5: // 一键开启台灯等
                              bleKeyboard.press(KEY_LEFT_SHIFT);
                              bleKeyboard.releaseAll();
                              delayMicroseconds(10);

                              readSD_off_state();
                              USBdata_refresh.oled = 1;
                              tx_onedata_to_USB();
                              tx_lampdata_to_SmartLamp("KB");
                              break;
                        }
                  }
            }
            vTaskDelay(10);
      }
}

void dict_key(int anger)
{
      bleKeyboard.rotate(anger);
}
void dict_press()
{
      bleKeyboard.pressDial();
}
void dict_release()
{
      bleKeyboard.releaseDial();
}
